package wpRelationsSandboxLinks;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import processing.core.PApplet;
import processing.xml.XMLElement;



public class WHub {
	protected PApplet parent;
	protected String title;
	public ArrayList<wArticle> links;
	protected static String QUERY_BASE_0 = "http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=";
	protected static String QUERY_BASE_1 = "&prop=links&pllimit=500";

	public WHub(PApplet theParent, String theTitle) {
		parent = theParent; 
		title = theTitle;
	}
	
	/*public Arraylist mylinks(){
		
		
	}*/
	
	
	
	
	public void searchLinks() {
		parent.fill(255);
		parent.ellipse(20, 20, 20, 20);
	
		String url = encodeURL(QUERY_BASE_0+title+QUERY_BASE_1);
		
		XMLElement xmlObj = new XMLElement(parent, url);
	    // ArrayList categories = new ArrayList();
	    if (xmlObj.hasChildren()) {
	        // get titles of all linked pages
	        XMLElement[] children = xmlObj.getChildren("query/pages/page/links/pl");
	        int nr = children.length; //xmlObj.getChildCount(); 
	        System.out.println("nr: " + nr); 
	        for (int i = 0; i < children.length; i++) {
	          String title = children[i].getStringAttribute("title");
	          //System.out.println(title); 
        	  links.add(new wCategory(parent,title));  

	          
	        }
	    }
		
		
	}

	public String encodeURL(String url) {

		  // Use URI to encode low Ascii characters depending on context of various parts
		  // For some reason, uri = new URI(url) chokes on space, so we have to split the URL
		  String scheme = null;  // http, ftp, etc.
		  String ssp = null; // scheme-specific part
		  String fragment = null; // #anchor for example
		  int colonPos = url.indexOf(":");
		  if (colonPos < 0) return "Not an URL";
		  scheme = url.substring(0, colonPos);
		  ssp = url.substring(colonPos + 1);
		  int fragPos = ssp.lastIndexOf("#");
		  if (fragPos >= 0) {
		    // Won't work if there is no real anchor/fragment
		    // but this char is part of one parameter of the query,
		    // but it is a bit unlikely...
		    // That's probably why Java doesn't want to do it automatically,
		    // it must be disambiguated manually
		    fragment = ssp.substring(fragPos + 1);
		    ssp = ssp.substring(0, fragPos);
		  }

		  URI uri = null;
		  try {
		    uri = new URI(scheme, ssp, fragment);
		  } 
		  catch (URISyntaxException use) { 
		    return use.toString(); 
		  }
		  String encodedURL1 = null;
		  try {
		    encodedURL1 = uri.toURL().toString();
		  } 
		  catch (MalformedURLException mue) { 
		    return mue.toString(); 
		  }
		  // Here, we still have Unicode chars unchanged

		  byte[] utf8 = null;
		  // Convert whole string to UTF-8 at once: low Ascii (below 0x80) is unchanged, other stuff is converted
		  // to UTF-8, which always have the high bit set.
		  try {
		    utf8 = encodedURL1.getBytes("UTF-8");
		  } 
		  catch (UnsupportedEncodingException uee) { 
		    return uee.toString(); 
		  }

		  StringBuffer encodedURL = new StringBuffer();

		  byte[] conv = new byte[1];
		  for (int i = 0; i < utf8.length; i++) {
		    // Beyond Ascii: high bit is set, hence negative byte
		    if (utf8[i] < 0) {
		      encodedURL.append("%" + Integer.toString(256 + (int)utf8[i], 16));
		    }
		    else {
		      conv[0] = utf8[i];
		      try {
		        encodedURL.append(new String(conv, "ASCII")); // Convert back to Ascii
		      } 
		      catch (UnsupportedEncodingException uee) { 
		        return uee.toString(); 
		      }
		    }
		  }

		  return encodedURL.toString();
		}
	
	
	
}
